<?php

/*
Info: Checks whether there is a new notification since the last ajax call
Author: Pritam Pebam
Version: 1.0
Contact: ptamzz@gmail.com; @ptamzz
URL: http://12bubbles.com
*/

	session_start();
	require_once('../inc/fnc.php');
	
	if(is_logged_in()) {
		//For alert
		$noti = (isset($_SESSION['lastcall']))? "AND crtdtime > " . $_SESSION['lastcall'] : "";
		
		$query = "SELECT * FROM `notification` WHERE `followerid` = {$_SESSION['userId']} {$noti} AND `delflag` = 'N' ORDER BY `crtdtime` DESC LIMIT 1";
		$result = mysql_query($query, $connection);
		$return['alert'] = (mysql_num_rows($result) > 0) ? true : false;

		//For messages
		$query = "SELECT * FROM `msgtable` WHERE (`senderid` = {$_SESSION['userId']} OR `receiverid` = {$_SESSION['userId']}) {$noti} AND `delflag` = 'N' ORDER BY `crtdtime` DESC LIMIT 1";
		$result = mysql_query($query, $connection);
		$return['newmsg'] = (mysql_num_rows($result) > 0) ? true : false;

		$return['status'] = true;
		$row = mysql_fetch_array($result);
		echo "DB " . $row['crtdtime'] . "; PHP: " . $_SESSION['lastcall'];

	} else {
		$return['status'] = false;
	}
	
	$_SESSION['lastcall'] = time();	//Setting new time for next notification ajax call

	echo json_encode($return);

?>